package com.caineng.zhaobiao.zbapi.utils;

import cn.hutool.json.JSONUtil;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liandyao
 * @date 2021/7/3 18:49
 */
public class StringUtils   {
    /**
     * 将对象转换为json
     * @param obj
     * @return
     */
    public static String toJson(Object obj){
        return  JSONUtil.toJsonStr(obj);
    }

    /**
     * 获取所有字段为null的属性名
     * 用于BeanUtils.copyProperties()拷贝属性时，忽略空值
     * @param source
     * @return
     */
    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 距凌晨23:59:59还剩下多少秒
     * @return long
     */
    public static Long timesCountDay()   {
        long now = System.currentTimeMillis();
        SimpleDateFormat sdfOne = new SimpleDateFormat("yyyy-MM-dd");
        long overTime = 0;
        try {
            //已经过了多少秒
            overTime = (now - (sdfOne.parse(sdfOne.format(now)).getTime()))/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        //当前时间  距离当天晚上23:59:59  秒数 也就是今天还剩多少秒
        long timeNext = 24*60*60 - overTime;
        return timeNext;

    }

    public static final String REGEX_MOBILE = "(13[0-9]|14[0-9]|15[0-9]|16[0-9]|17[0-9]|18[0-9]|19[0-9])\\d{8}";
    static  Pattern p = Pattern.compile(REGEX_MOBILE);
    /**
     * 手机号码是否正确
     * @param phone
     * @return 如果正确返回ture
     */
    public static boolean isTruePhone(String phone){
        if(phone==null) return false;

        Matcher m = p.matcher(phone);
        return m.matches() ;
    }

    /**
     * 替换手机号码为********789,保留后三位
     * @param content
     * @return
     */
    public static String regexMobile(String content){
        Matcher m = p.matcher(content);
        String paramStr = new String(content);
        while (m.find()) { //一定需要先查找再调用group获取电话号码
            paramStr = paramStr.replaceAll(m.group(), "****");
        }
        return paramStr;
    }





    public static void main(String[] args) {
        String str=null;
        boolean s = isTruePhone(str);
        System.out.println(s);
    }
}
